package com.uenpay.im.ui.old.activity.chat;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.qyx.android.weight.choosemorepic.PhotoItem;
import com.uenpay.im.IMApplication;
import com.uenpay.im.R;
import com.uenpay.im.ui.old.activity.choosemorepic.PhotoActivity;
import com.uenpay.im.entities.SendFile;
import com.uenpay.util.app.base.BaseActivity;

import java.io.File;
import java.util.ArrayList;

/**
 * @author SL
 * @date 2014/7/23
 * @copyRight http://www.qiyunxin.com
 */
public class ChooseSendFileActivity extends BaseActivity {

    private final int CHOOSE_MUSIC_CODE = 100;
    private final int CHOOSE_PIC_CODE = 110;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        isFullScreen = true;
        super.onCreate(savedInstanceState);
    }

    @Override
    public View bindView() {
        return null;
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_choose_send_file_type;
    }

    @Override
    public void initView(View view) {
        ((TextView) findViewById(R.id.title_textview)).setText(getResources()
                .getString(R.string.local_file));
        /**
         */
        findViewById(R.id.choose_image_file_layout).setOnClickListener(
                new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        Bundle bundle = new Bundle();
                        bundle.putInt("selected_count", 0);
                        bundle.putInt("max_select_count", 1);
                        ActivityHandle.startActivity(
                                ChooseSendFileActivity.this,
                                PhotoActivity.class, bundle, CHOOSE_PIC_CODE);
                    }
                });

        /**
         */
        findViewById(R.id.choose_music_file_layout).setOnClickListener(
                new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        Bundle bundle = new Bundle();
                        bundle.putBoolean("is_music", true);
                        ActivityHandle.startActivity(
                                ChooseSendFileActivity.this,
                                LocalMusicActivity.class, bundle,
                                CHOOSE_MUSIC_CODE);
                    }
                });
        findViewById(R.id.choose_download_file_layout).setOnClickListener(
                new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        ActivityHandle.startActivity(
                                ChooseSendFileActivity.this,
                                LocalMusicActivity.class, null,
                                CHOOSE_MUSIC_CODE);
                    }
                });

        findViewById(R.id.choose_other_file_layout).setOnClickListener(
                new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        ActivityHandle.startActivity(
                                ChooseSendFileActivity.this,
                                FolderListActivity.class, null,
                                CHOOSE_MUSIC_CODE);
                    }
                });
        //返回键点击
        findViewById(R.id.back_layout).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                finish();
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            if (requestCode == CHOOSE_MUSIC_CODE) {
                SendFile mSendFile = data.getParcelableExtra("send_file");
                data.putExtra("send_file", mSendFile);
                setResult(RESULT_OK, data);
                ChooseSendFileActivity.this.finish();
            } else if (requestCode == CHOOSE_PIC_CODE) {
                @SuppressWarnings("unchecked")
                ArrayList<PhotoItem> select_gl_arr = (ArrayList<PhotoItem>) data
                        .getSerializableExtra("gl_arr");
                if (select_gl_arr.size() > 0) {
                    for (int i = 0; i < select_gl_arr.size(); i++) {
                        String path = select_gl_arr.get(i).getPath();

                        File file = new File(path);

                        long mSize = file.length();
                        if (mSize > 0) {
                            String mName = file.getName();
                            SendFile mSendFile = new SendFile();
                            mSendFile.filePath = path;
                            mSendFile.fileSize = mSendFile.getFileSize(mSize
                                    + "");
                            mSendFile.fileName = mName;

                            data.putExtra("send_file", mSendFile);
                            setResult(RESULT_OK, data);
                        }
                        finish();
                    }
                }
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

}
